package cn.wolfcode.shop.cloud.mq.listener;

import cn.wolfcode.shop.cloud.common.JSONUtil;
import cn.wolfcode.shop.cloud.mq.MQConstants;
import cn.wolfcode.shop.cloud.mq.msg.DelaySeckillOrderMsg;
import cn.wolfcode.shop.cloud.service.IOrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by wolfcode-lanxw
 */
@Component
@Slf4j
@RocketMQMessageListener(
        topic = MQConstants.SECKILL_ORDER_TOPIC,
        selectorExpression = MQConstants.DELAY_SECKILL_ORDER_TAG,
        consumerGroup = MQConstants.DELAY_SECKILL_ORDER_CONSUMER_GROUP
)
public class DelaySeckillOrderMQListener implements RocketMQListener<DelaySeckillOrderMsg> {

    @Autowired
    private IOrderInfoService orderInfoService;

    @Override
    public void onMessage(DelaySeckillOrderMsg message) {
        log.info("[延迟订单消息] 收到消息:{}", JSONUtil.toJSONString(message));
        // 调用 service 检查订单是否超时未支付
        orderInfoService.checkTimeout(message.getOrderNo(), message.getSeckillId());
    }
}
